package com.sp.system.user.controller;

import com.ananjt.system.entity.SpUser;
import com.netflix.client.http.HttpRequest;
import com.sp.system.user.core.utils.ContentResult;
import com.sp.system.user.security.OAuth2Utils;
import com.sp.system.user.service.Oauth2Service;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.oauth2.provider.token.ConsumerTokenServices;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;


@Slf4j
@RestController
@RequestMapping("/system")
public class TokenController {

    private Oauth2Service oauth2Service;


    @PostMapping("/login")
    public Map<String, Object> login(String loginName, String password) {
        Map<String, String> parameters = new HashMap<>();
        parameters.put(OAuth2Utils.GRANT_TYPE, "password");
        parameters.put(OAuth2Utils.CLIENT_ID, "cah");
        parameters.put("client_secret", "secret");
        parameters.put(OAuth2Utils.SCOPE, "all");
        parameters.put("username", loginName);
        parameters.put("password", password);

        Map<String, Object> tokenInfo = oauth2Service.getAccessToken(parameters);
        log.debug("{}用户名密码登录", loginName);
        return tokenInfo;
    }

    @GetMapping("/removeToken")
    public Object removeToken(String accessToken){

        return oauth2Service.removeToken(accessToken);
    }

    @Autowired
    public void setOauth2Service(Oauth2Service oauth2Service) {
        this.oauth2Service = oauth2Service;
    }
}
